package com.game.primitive;

import com.game.net.server.jpbsocket.JPbSocketNettyServerHandler;
import com.game.message.Msg;
import com.game.message.connetion.ConnectionManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by legend9999 on 2016/4/25.
 */
@Component
@ChannelHandler.Sharable
public class PrimitiveJPbServerHandler extends JPbSocketNettyServerHandler {
	private static Logger logger = LoggerFactory.getLogger(PrimitiveJPbServerHandler.class);
	@Autowired
	private PrimitiveBiz primitiveBiz;

	@Override
	public void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
		primitiveBiz.onMsg(msg,ctx);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		ctx.attr(ConnectionManager.userIdAttributeKey).set(0);
	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		super.channelUnregistered(ctx);
		ConnectionManager.getInstance().remove(ctx);
	}
}

